package com.eedi.framework.logger.service;

import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.logger.controller.org.vo.OrgOperateLogPageReq;
import com.eedi.framework.logger.controller.org.vo.OrgOperateLogResp;
import com.eedi.framework.logger.controller.sys.vo.SysOrgOperateLogPageReq;
import com.eedi.framework.logger.dal.mapper.OrgLogOperateMapper;
import com.eedi.framework.logger.enmus.SysLogTypeEnum;
import com.eedi.org.facade.OrgInfoFacade;
import com.eedi.org.facade.OrgUserFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
@Slf4j
public class OrgLogOperateServiceImpl implements OrgLogOperateService {
    @Resource
    OrgLogOperateMapper orgLogOperateMapper;

    @Resource
    OrgUserFacade orgUserFacade;
    @Resource
    OrgInfoFacade orgInfoFacade;

    @Override
    public PageResult<OrgOperateLogResp> getOperateLogPage(OrgOperateLogPageReq pageReqVO,String orgInfoId) {
        // 处理基于用户昵称的查询
        Collection<String> userIds = null;
        if (StrUtil.isNotEmpty(pageReqVO.getUserNickname())) {
            userIds = orgUserFacade.findUserIdentification(pageReqVO.getUserNickname());
        }
        // 查询分页
        PageResult<OrgOperateLogResp> result = orgLogOperateMapper.selectPage(pageReqVO, userIds, SysLogTypeEnum.OPERATER_LOG, orgInfoId);
        orgUserFacade.fillInUserName(result.getList(), OrgOperateLogResp::getUserId, OrgOperateLogResp::setUserNickname);
        orgInfoFacade.fillInOrgName(result.getList(),OrgOperateLogResp::getOrgInfoId,OrgOperateLogResp::setOrgName);
        orgUserFacade.fillInUserLoginId(result.getList(),OrgOperateLogResp::getUserId,OrgOperateLogResp::setOrgUserAccountId);
        return result;
    }

    @Override
    public PageResult<OrgOperateLogResp> getOperateLogPage(SysOrgOperateLogPageReq pageReqVO, String tenantId) {
        // 处理基于用户昵称的查询
        Collection<String> userIds = null;
        if (StrUtil.isNotEmpty(pageReqVO.getUserNickname())) {
            userIds = orgUserFacade.findUserIdentification(pageReqVO.getUserNickname());
        }
        // 查询分页
        PageResult<OrgOperateLogResp> result = orgLogOperateMapper.selectPage(pageReqVO, userIds, SysLogTypeEnum.OPERATER_LOG, tenantId);
        orgUserFacade.fillInUserName(result.getList(), OrgOperateLogResp::getUserId, OrgOperateLogResp::setUserNickname);
        orgInfoFacade.fillInOrgName(result.getList(),OrgOperateLogResp::getOrgInfoId,OrgOperateLogResp::setOrgName);
        orgUserFacade.fillInUserLoginId(result.getList(),OrgOperateLogResp::getUserId,OrgOperateLogResp::setOrgUserAccountId);
        return result;
    }
}
